﻿using Dapper.SimpleDao.Entity;
using Dapper.SimpleDao.Factory;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Data;

namespace Dapper.SimpleDao.Infrastructure
{
    /// <summary>
    /// 数据仓储接口
    /// </summary>
    /// <typeparam name="T">数据实体</typeparam>
    public interface IRepository<T> where T :BaseEntitiy
    {
        /// <summary>
        /// 数据连接
        /// </summary>
        IDbContext Context { get; }

        /// <summary>
        /// 同一事务提交
        /// </summary>
        /// <param name="funcs">委托函数集合</param>
        bool Commit(IEnumerable<FuncParamater<T>> funcs);

        #region 同步代码
              /// <summary>
        /// 获取一个实体
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        T Get(string id);
        /// <summary>
        /// 添加一个实体
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        int Insert(T obj, IDbTransaction tran =null);
        /// <summary>
        /// 更新一个实体
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        int Update(T obj, IDbTransaction tran = null);
        /// <summary>
        /// 根据实体字段查询列表
        /// </summary>
        /// <typeparam name="TDbModel">返回的查询数据实体（与实体相同，只取需要的字段）</typeparam>
        /// <param name="where">字段查询条件</param>
        /// <param name="page">页数</param>
        /// <param name="size">条数</param>
        /// <param name="total">总数</param>
        /// <param name="sort">排序字段</param>
        /// <param name="op">字段查询条件拼接（1.支持全And。2.支持全Or。）</param>
        /// <returns></returns>
        IEnumerable<TDbModel> FindAll<TDbModel>(IEnumerable<FieldPredicate<T>> where, int page, int size, out int total, IEnumerable<PropertySort<T>> sort = null, GroupOperator op = GroupOperator.And)
               where TDbModel : class;
        /// <summary>
        /// 根据实体字段查询列表
        /// </summary>
        /// <typeparam name="TDbModel">返回的查询数据实体（与实体相同，只取需要的字段）</typeparam>
        /// <param name="where">字段查询条件</param>
        /// <param name="page">页数</param>
        /// <param name="size">条数</param>
        /// <param name="total">总数</param>
        /// <returns></returns>
        IEnumerable<TDbModel> FindAll<TDbModel>(IDictionary<string, object> where, int page, int size, out int total)
             where TDbModel : class;

        #endregion

        #region 异步代码
        /// <summary>
        /// 获取一个实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<T> GetAsync(string id);
        #endregion
    }
}
